﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Image2d
{
    class Shot : AnimatedObject2D
    {
        float velocity;
        float time;
        Vector2 direction;

        Vector2 clientBounds;

        string type;

        public Shot(Texture2D image, Vector2 position, Animation[] animations,
            float rotation, float velocity, float time, Vector2 bounds, string type)
            : base(image, position, animations)
        {
            this.Rotation = rotation;
            this.velocity = velocity;
            this.time = time;
            this.type = type;
            Origin = new Vector2(Rect.Width / 2, Rect.Height+25);
            direction = new Vector2((float)Math.Sin(rotation), -(float)Math.Cos(rotation));

            clientBounds = bounds;
        }

        public void Update()
        {
            
            
        }


        public bool CheckUpdate(GameTime gameTime, Vector2 pos, float rot)
        {
            if (type == "normal")
            {
                Position += direction * velocity;
                CheckBounds();
                base.Update(gameTime);

                if (time + 1000 <
                    (float)gameTime.TotalGameTime.TotalMilliseconds)
                    return true;
                else
                    return false;
            }
            else
            {
                this.Rotation = rot;
                this.Position = pos;
                if (time + 1000 <
                    (float)gameTime.TotalGameTime.TotalMilliseconds)
                    return true;
                else
                    return false;
            }
        }

        public void CheckBounds()
        {
            //se ele sair esquerda vai para direita
            if (Position.X < 0 - this.currentAnimation.frameSize.X / 2)
            {
                Position = new Vector2(clientBounds.X + this.currentAnimation.frameSize.X / 2, Position.Y);
            }
            //se ele sair esquerda vai para direita
            if (Position.X > clientBounds.X + this.currentAnimation.frameSize.X / 2)
            {
                Position = new Vector2(0 - this.currentAnimation.frameSize.X / 2, Position.Y);
            }
            //cima vai para baixo
            if (Position.Y < 0 - this.currentAnimation.frameSize.Y / 2)
            {
                Position = new Vector2(Position.X, clientBounds.Y + this.currentAnimation.frameSize.Y / 2);
            }
            //baixo vai para cima
            if (Position.Y > clientBounds.Y + this.currentAnimation.frameSize.Y / 2)
            {
                Position = new Vector2(Position.X, 0 - this.currentAnimation.frameSize.Y / 2);
            } 
        }
    }
}
